<?php 
function qq_callback(){
        //curl获取页面内容, 直接输出
        function contents($url){
            $ch = curl_init();                              // 初始化一个CURL会话
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($ch,CURLOPT_URL,$url);
            $str = curl_exec($ch);
            curl_close($ch);
            return $str;
        }

        require_once("apios.php");

        $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"."client_id=".$_COOKIE["janesid"]."&redirect_uri=".urlencode($_COOKIE["callback"])."&client_secret=".$_COOKIE["janeskey"]."&code=".@$_REQUEST["code"];
        //获取配置选项的值
        if(ini_get("allow_fopen_url")=="1"){
            $response = file_get_contents($token_url);
        }else{
            $response = contents($token_url);
        }

        if (strpos($response, "callback") !== false){
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response  = substr($response, $lpos + 1, $rpos - $lpos -1);
            $msg = json_decode($response);
        }

        $params = array();
        parse_str($response, $params);
        $access_token = @$params["access_token"];


        $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$access_token;
        if(ini_get("allow_fopen_url")=="1"){
            $str = file_get_contents($graph_url);
        }else{
            $str = contents($graph_url);
        }
        if (strpos($str, "callback") !== false){
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str  = substr($str, $lpos + 1, $rpos - $lpos -1);
        }

        $user = json_decode($str);

        $openid = @$user->openid;
		
  
  		$url = "https://graph.qq.com/user/get_user_info?access_token=".$access_token."&oauth_consumer_key=".$_COOKIE["janesid"]."&openid=".$openid;
        if(ini_get("allow_fopen_url")=="1"){
            $user_info = file_get_contents($url);
        }else{
            $user_info = contents($url);
        }

        $user_info = json_decode($user_info,true);
  	  $nickname = @$user_info["nickname"];
  
  
  		if($_COOKIE['TYPE'] == 'admin'){
        	header("location:http://".$_COOKIE["HOST"]."/Admin_Index/index/login/openid/".$openid);
            exit();
        }else if($_COOKIE['TYPE'] == 'build'){
        	header("location:http://".$_COOKIE["HOST"]."/Admin_Index/System/build/openid/".$openid."/access_token/".$nickname."/id/".$_COOKIE['BUILD']."/from/".$_COOKIE['FROM']);
          	exit();
        }else if($_COOKIE['TYPE'] == 'shanghu'){
        	header("location:http://".$_COOKIE["HOST"]."/main.php/index/login/openid/".$openid);
            exit();
        }else if($_COOKIE['TYPE'] == 'shbuild'){
        	header("location:http://".$_COOKIE["HOST"]."/main.php/index/build/openid/".$openid."/access_token/".$access_token."/id/".$_COOKIE['BUILD']."/from/".$_COOKIE['FROM']);
          	exit();
        }else if($_COOKIE['TYPE'] == 'vipadmin'){
        	header("location:http://".$_COOKIE["HOST"]."/Vip_Index/index/login/openid/".$openid);
            exit();
        }else if($_COOKIE['TYPE'] == 'supbuild'){
        	header("location:http://".$_COOKIE["HOST"]."/admin/index.php/system/build/openid/".$openid."/access_token/".$nickname."/id/".$_COOKIE['BUILD']."/from/".$_COOKIE['FROM']);
          	exit();
        }else if($_COOKIE['TYPE'] == 'supadmin'){
        	header("location:http://".$_COOKIE["HOST"]."/admin/index/login/openid/".$openid);
            exit();
        }else if($_COOKIE['TYPE'] == 'supindexbuild'){
        	header("location:http://".$_COOKIE["HOST"]."/index.php/Users/build/openid/".$openid."/access_token/".$access_token."/name/".$nickname."");
          	exit();
        }else if($_COOKIE['TYPE'] == 'supindex'){
        	header("location:http://".$_COOKIE["HOST"]."/index.php/index/login/openid/".$openid."");
            exit();
        }else if($_COOKIE['TYPE'] == 'skbs'){
        	header("location:http://".$_COOKIE["HOST"]."/qq/ok.php?id=".$openid."");
            exit();
        }
        
  
  
  
        $url = "https://graph.qq.com/user/get_user_info?access_token=".$access_token."&oauth_consumer_key=".$_COOKIE["janesid"]."&openid=".$openid;
        if(ini_get("allow_fopen_url")=="1"){
            $user_info = file_get_contents($url);
        }else{
            $user_info = contents($url);
        }

        $user_info = json_decode($user_info,true);
  
  	  $nickname = @$user_info["nickname"];
  
  
        if(!$openid){
            echo $user_info['msg'];
        }else{
            header("location:http://".$_COOKIE["HOST"]."/index.php/recharge/qq/jane/".sha1("huachi")."/openid/".$openid."/access_token/".$access_token."/user_name/".$nickname."/qqapi/".md5("huachi")."");
        }
}

header("Content-Type:text/html;charset=utf-8");
$state = isset($_REQUEST['state'])?$_REQUEST['state']:"Jane";
if($state == md5("huachi")){
    qq_callback();
}else{
    exit("请重新登录");
}

?>
